class Entry < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations
  has_many :comments
  belongs_to :user
  before_validation :create_permalink
  
  validates_presence_of :title, :permalink
  validates_uniqueness_of :body, :title
  validates_associated :user
  
  def category_list
    self.categories.collect { |category| "<a href=\"/categories/#{category.id}\">#{category.name}</a>" }.compact.to_sentence(:skip_last_comma => true)
  end

  # inspired by http://svn.techno-weenie.net/projects/plugins/permalink_fu/lib/permalink_fu.rb
  def create_permalink
    self.permalink = title.gsub(/\W+/, ' ').strip.downcase.gsub(/\ +/, '-') if permalink.to_s.empty?
    logger.info permalink
  end
  
end
